﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.huawei.esdk.tp.professional.eSDKPlatformKeyMgrService;
using System.Web.Services.Protocols;
using com.huawei.esdk.tp.professional.Utils;
using com.huawei.esdk.tp.professional.DataType;
using com.huawei.esdk.tp.professional.eSDKTPProfessionalConfMgr;

namespace com.huawei.esdk.tp.professional.local
{
    public partial class PlatformKeyServiceProvider
    {
        private static string url = com.huawei.esdk.tp.professional.Utils.UtilsSettings.URL + com.huawei.esdk.tp.professional.Utils.UtilsSettings.platformKeyMgr;
        private static PlatformKeyMgrService platformKey = new PlatformKeyMgrService(url);

        private static PlatformKeyServiceProvider instance = null;
        protected PlatformKeyServiceProvider()
        {

        }

        public static PlatformKeyServiceProvider Instance()
        {
            if (null == instance)
            {
                instance = new PlatformKeyServiceProvider();
            }
            return instance;
        }

        public TPSDKResponseEx<string> getPublicKey()
        {
            TPSDKResponseEx<string> resp = new TPSDKResponseEx<string>();
            try
            {
                string secretType = "RSA2048";
                string publicKey = string.Empty;
                resp.resultCode = platformKey.getPublicKey(secretType, out publicKey);
                if (resp.resultCode == 0)
                    resp.result = publicKey;
                return resp;
            }
            catch (SoapHeaderException e)
            {
                resp.resultCode = ExceptionUtils.processSoapException(e);
                return resp;
            }
        }

        public int setSecretKey(string secretKey, string iv)
        {
            try
            {
                platformKey.CookieContainer = AuthorizeServiceEx.cookieJar;
                string secretType = "AES128";
                int result = platformKey.setSecretKey(secretType, secretKey, iv);
                return result;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }
        }
    }
}
